//
//  AfterDayTableViewCell.m
//  WWTQ
//
//  Created by JP on 2018/10/30.
//  Copyright © 2018年 Jqert. All rights reserved.
//

#import "AfterDayTableViewCell.h"

#import "AfterDayCollectionViewCell.h"

#import "ForecastModel.h"

@interface AfterDayTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *forecastArr;

@end

static NSString *AfterDayCollectionViewCellID = @"AfterDayCollectionViewCellID";

@implementation AfterDayTableViewCell

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = layout.minimumInteritemSpacing = 0;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        
        [_collectionView registerClass:[AfterDayCollectionViewCell class] forCellWithReuseIdentifier:AfterDayCollectionViewCellID];
    }
    return _collectionView;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    [self addSubview:self.collectionView];
    self.collectionView.frame = CGRectMake(0, 0, ScreenW, 230);
}

- (void)setDataModel:(DataObjectModel *)dataModel {
    _dataModel = dataModel;
    
    self.forecastArr = [NSArray arrayWithArray:dataModel.forecast];
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.forecastArr.count;
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    AfterDayCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:AfterDayCollectionViewCellID forIndexPath:indexPath];
    cell.model = self.forecastArr[indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(ScreenW/3, 230);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DeLog(@"indexPath.row = %ld, indexPath.section = %ld", indexPath.row, indexPath.section);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


@end
